package org.microframework.cloud.logs.controller;

import org.microframework.base.core.domain.R;
import org.microframework.base.core.util.IpUtil;
import org.microframework.cloud.logs.model.domain.OperationLog;
import org.microframework.cloud.logs.service.ILoginLogService;
import org.microframework.cloud.logs.service.IOperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

/**
 * 日志服务REST控制器
 * 用于接收其他服务的日志记录请求
 *
 * @author zs
 * @version 1.0.0
 * @since 2024-06-14
 */
@Slf4j
@RestController
@RequestMapping("/log")
@Tag(name = "日志服务", description = "日志服务接口，用于接收其他服务的日志记录请求")
public class LogController {

    @Autowired
    private ILoginLogService loginLogService;
    
    @Autowired
    private IOperationLogService operationLogService;
    
    /**
     * 记录登录成功日志
     */
    @PostMapping("/login/success")
    @Operation(summary = "记录登录成功日志", description = "记录用户登录成功的日志信息")
    public R<Boolean> recordLoginSuccess(
            @Parameter(description = "用户名") @RequestParam("username") String username, 
            HttpServletRequest request) {
        try {
            String ipAddress = IpUtil.getIpAddr(request);
            String userAgent = request.getHeader("User-Agent");
            boolean result = loginLogService.recordLoginSuccess(username, ipAddress, userAgent);
            return R.ok(result);
        } catch (Exception e) {
            log.error("记录登录成功日志失败", e);
            return R.fail("记录登录日志失败");
        }
    }
    
    /**
     * 记录登录失败日志
     */
    @PostMapping("/login/failure")
    @Operation(summary = "记录登录失败日志", description = "记录用户登录失败的日志信息")
    public R<Boolean> recordLoginFailure(
            @Parameter(description = "用户名") @RequestParam("username") String username, 
            @Parameter(description = "错误信息") @RequestParam("errorMessage") String errorMessage, 
            HttpServletRequest request) {
        try {
            String ipAddress = IpUtil.getIpAddr(request);
            String userAgent = request.getHeader("User-Agent");
            boolean result = loginLogService.recordLoginFailure(username, errorMessage, ipAddress, userAgent);
            return R.ok(result);
        } catch (Exception e) {
            log.error("记录登录失败日志失败", e);
            return R.fail("记录登录日志失败");
        }
    }
    
    /**
     * 保存操作日志
     */
    @PostMapping("/save")
    @Operation(summary = "保存操作日志", description = "保存系统操作日志信息")
    public R<Boolean> saveLog(
            @Parameter(description = "操作日志对象") @RequestBody OperationLog operLog, 
            @Parameter(description = "是否内部调用") @RequestParam(value = "inner", required = false) String inner) {
        try {
            boolean result = operationLogService.insertOperationLog(operLog);
            return R.ok(result);
        } catch (Exception e) {
            log.error("保存操作日志失败", e);
            return R.fail("保存操作日志失败");
        }
    }
} 